package org.example.service.impl;

import org.example.javabean.Role;
import org.example.mapper.RoleMapper;
import org.example.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
public class RoleServiceImpl implements RoleService {
    @Autowired
    RoleMapper roleMapper;
    @Override
    public List<Role> findAll() {
        return roleMapper.selectAll();
    }

    @Override
    public void add(Role role) {
        roleMapper.insert(role);
    }

    @Override
    @Transactional
    public void deleteById(int id) {
        //先删除角色权限关联表数据再删除角色表数据
        roleMapper.deleteByRid(id);
        roleMapper.deleteById(id);
    }

    @Override
    public void deleteByList(ArrayList<Integer> ids) {
        roleMapper.deleteR_PByList(ids);
        roleMapper.deleteByList(ids);
    }

    @Override
    public Role findR_PById(int id) {
        return roleMapper.selectR_PById(id);
    }

    @Override
    public void edit(Role role) {
        roleMapper.updateById(role);
    }

    @Override
    @Transactional
    public void editPermission(Integer rid,List<Integer> ids) {
        roleMapper.deleteR_PById(rid);
        roleMapper.insertR_P(rid,ids);
    }
}
